<template>
	<!-- 回收网点页面 -->
	<view class="map">
		<map class="map" :latitude="latitude" :longitude="longitude" :markers="covers" scale="13"
			:showLocation="true"></map>
	</view>
</template>

<script>
	import util from '../../component/commen/api/util'
	export default {
		data() {
			return {
				latitude: "", //经度
				longitude: "", //维度
				covers: [], //标记点
			}
		},
		onShow() {
			this.packIndexPackList()
			this.init()
		},
		methods: {
			packIndexPackList() { //获取网点信息
				var _this = this
				var params = {
					do: "pack",
					op: "indexPackList"
				}
				util.requestWithSign(params).then(res => {
					console.log("回收网点信息", res)
					_this.covers = []; // 清空 covers 数组
					res.data.forEach(item => {
						var location = {
							latitude: item.lat,
							longitude: item.lng,
							iconPath: "https://huishou.68699.cn/addons/wjyk_recycle/public/static/index/pack.png",
							width: 40,
							height: 40,
							callout: {
								content: item.name,
								color: "#FFFFFF",
								fontSize: 10,
								borderRadius: 5,
								bgColor: "#3FCB25",
								display: "ALWAYS",
								padding: 3,
								textAlign: "center"
							}
						};
						_this.covers.push(location);
					})
				})
			},
			init() { //获取当前用户的位置信息
				var that = this
				uni.getLocation({
					type: "wgs84",
					success(res) {
						console.log("我的位置信息", res)
						that.latitude = res.latitude
						that.longitude = res.longitude
					},
					fail(err) {
						uni.showModal({
							title: '提示',
							content: '请开启定位权限以获取位置信息',
							showCancel: true,
							success(res) {
								if (res.confirm) {
									// 用户点击确定，跳转至设置页面
									uni.openSetting({
										success(res) {
											console.log('openSetting success', res
												.authSetting);
											// 用户授权成功后再次尝试获取位置信息
											if (res.authSetting[
													'scope.userLocation']) {
												that.init();
											}
										}
									});
								}
							}
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.map {
		width: 750rpx;
		height: 100vh;
	}
</style>